
//initial function UI
(function ($) {
    $.fn.initialUI = function (settings) {
        var holder = $(this); // table holder
        var defaults = {
        //            classSeletorTR: "newData",
        //            classSeletorInput: "canEditInTable",
    };
    var options = jQuery.extend(defaults, settings);
    InitialButton(holder, options);
    MakeDatepicker(holder);
    //InitialAutoCompleteTextOpenDlg(holder);
    InitialEl(holder, options);
    //InitialTokenInput(holder);
    function InitialButton(holder, options) {
         $(holder).find(".ElBtn").each(function () {
            try{
                if($(this).find(".ui-button-text").size() > 0)
                {
                    var caption = $(this).find(".ui-button-text").text();
                    $(this).find(".ui-button-text").remove();
                    $(this).text(caption);
                }
                if($.type($(this).attr("jIcon")) !== 'undefined' && $(this).attr("jIcon") !== ""){
                    var jIcon = $(this).attr("jIcon");
                    //alert(jIcon);
                    $(this).button({ icons: { primary: 'ui-icon '+jIcon} });
                }else{
                    $(this).button();
                }
                //assign event
                if($(this).attr("jclick") != "")
                {
                    var functionName = $(this).attr("jclick");
                    if(functionName != undefined && functionName != ""){
                        $(this).click(function(){
                            eval(functionName) ($(this));
                        });
                    }
                }
                $(this).button('enable');
                if($(this).hasClass("disable"))
                {
                    $(this).button('disable');
                    $(this).removeClass("disable");
                }
            }
            catch(e)
            {
                console.log(e.toString());
            }
        });
        //Initail jButton
         $(holder).find(".jButton").each(function(){
            AssignEventButton($(this),options);
            var jdisable = $(this).attr("jdisable");
            if(jdisable != "")
            {
                jdisable = jdisable.toLowerCase();
            }
            console.log(jdisable);
            if(jdisable == 'true'){
                $(this).jButton('disable');
            }
            else{ //function มีผลกับ style=Display:none
                var display = $(this).css("display");
                $(this).jButton('enable');
                if(display == "none"){
                    $(this).hide();
                }
            }
         });
         $(holder).find(".ElBtnImageSetting").each(function () {
            $(this).button({ icons: { primary: 'ui-icon-wrench'} });
        });
    }
    // end InitialButton 
    function InitialEl(holder, options) {
        //MakeDatepickerII(holder);
        $(holder).AjexUpdateDataInElement({ isGetDataFromEl: true });
    }
    function AssignEventButton(holder, options){
        if($(holder).attr("jclick") != "")
        {
            var functionName = $(holder).attr("jclick");
            if(functionName != undefined && functionName != ""){
                $(holder).click(function(){
                    var n = functionName.search("()");
                    if(n > 0){
                        eval(functionName) ();
                    }else{
                        eval(functionName) ($(holder));
                    }
                });
            }
        }
    }
    //make disable and enable for jquery button
    function DisableButton(holder,options)
    {
        var jdisable = $(holder).attr("jdisable");
        if(jdisable == "" || jdisable == undefined)
        {
            return false;
        }
        jdisable = jdisable.toLowerCase();
        console.log(jdisable);
        if(jdisable == 'true'){
            $(holder).button('disable')
        }else{
            $(holder).button('enable');
        }
    }
                

}; //end plugin
})(jQuery);
function InitialTokenInput(holder)
{
    $(holder).find(".ELTokenInput-Section").each(function(){
        var jsonData = jQuery.parseJSON($(this).find("input.ELTokenInput-DefaultVal").val());
        var url = $(this).find("input.ELTokenInput-URL").val();
        $(this).find(".tokenInput").tokenInput(url, {
            theme: "facebook",
            prePopulate: jsonData,
        });
    });
}

function InitialAutoCompleteTextOpenDlg(holder) {
    $(holder).find(".autoCompleteElTextOpenDlg").each(function () {
        var ref_textDlgID = $(this).attr("ref_textDlgID");
        var ref_textValueID = $(this).attr("ref_textValueID");
        var callback = "";
        var callbackOnSelect = "";
        var sendKeyToElement = ref_textValueID;
        var displayFormat = "";
        var obj = $(this).val();
        var jsonModel = jQuery.parseJSON(obj);
        var jsonData = jsonModel.autoCompleteOption;
        var dataSend = "";
        
        if (jsonData != null) {
            if (jsonData.dataSend != null && jsonData.dataSend != "") {
                try {
                    dataSend = jQuery.parseJSON(jsonData.dataSend);
                } catch (e) {
                    alert("error at function InitialAutoCompleteTextOpenDlg - " + e.Message + "| incorrect json ->" + jsonData.dataSend);
                }
            }
            sendKeyToElement = (jsonData.sendKeyToElement != null) ? jsonData.sendKeyToElement : ref_textValueID;
            callback = (jsonData.callback != null) ? jsonData.callback : "";
            callbackOnSelect = (jsonData.callbackOnSelect != null) ? jsonData.callbackOnSelect : "";
            displayFormat = (jsonData.displayFormat != null) ? jsonData.displayFormat : "";
            $("#" + ref_textDlgID).tmisAutoComplete({
                url: jsonData.url,
                data: dataSend,
                callback: callback, callbackOnSelect: callbackOnSelect, maxRow: jsonData.maxRow, SendKeyToElement: "#" + sendKeyToElement,
                displayFormat: displayFormat,isSelectOnly:true,
            });
           
            
            $("#" + ref_textDlgID).blur(function () {
                if (jsonData.isCheckACNotNull == true)
                {
                    delayID = setTimeout(function () {
                        IsCheckACNoData($("#"+ ref_textDlgID),jsonData);
                        //delete text in autocomplete. it's will delete code
                        try{
                            if($(this).val() != "undefined"){
                                if ($.trim($(this).val()) == "") {
                                    $("#" + ref_textValueID).val("");
                                }
                            }
                        }
                        catch(e)
                        {
                            console.log(e.toString());
                        }
                    }, 800);
                }
                
                
            });

        }
    });//end loop

}

function InitailTextOpenDlg(jsonModel) {
    
}

function IsCheckACNoData(holderTextAuto,options)
{
    var dataSend = jQuery.parseJSON(options.dataSend);
    if ($.trim($(holderTextAuto).val()) != "") {
        var textValue = $.trim($(holderTextAuto).val());
        var isNoData = true;
        //get data from tmisAutoComplete.js
        var oldData = $("body").data("tempAC");
        if(oldData == "" || oldData == undefined)
        {
            isNoData = false;
        }
        $(oldData).find("li.liAutoComplete").each(function(){
            //alert($(this).attr("values"));
            if(textValue == $(this).attr("values") || textValue == $(this).attr("text")){
                isNoData = false;
                //+ "values='" + jsonData.ListItem[i].Value
                //+ "' text='"+ jsonData.ListItem[i].Text
                return false;
            }
        });
        if(isNoData == true)
        {
            $(holderTextAuto).val("");
            alert("No data.");
        }
        $(".autoComplete").remove();
        $("body").data("tempAC","");
    }
}
